package com.baihuabooks.scoresys.servlets;

import com.baihuabooks.scoresys.entity.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Thinkpad on 2016/5/17 0017.
 */
public class AppFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpSession session = httpServletRequest.getSession(true);
        Object obj = session.getAttribute("user");
        if(obj == null) {

            System.out.println(httpServletRequest.getRequestURI());
            if(!httpServletRequest.getRequestURI().contains("login.html")) {
                request.getRequestDispatcher("/index.jsp").forward(request, response);
                return;
            }

        }

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        chain.doFilter(request, response);
    }

    public void destroy() {

    }
}
